
from astropy import units as u
from astropy.time import Time
from netorchestr.envir.base import ONet
from netorchestr.envir.node.ground import GroundServerBase
from netorchestr.envir.node.pedestrian import CrowdBase
from netorchestr.envir.node.controller import ControllerGlobal4Crowd
from netorchestr.envir.physicallayer import RadioMedium

if __name__ == '__main__':
    net = ONet('GroundServerSfcs')
    
    init_time = Time("2021-07-22 00:00:00")

    vim_1 = GroundServerBase('Vim1', init_time, [113.5100, 34.7550, 0.0])
    vim_1.switch.networkLayer.ip_addr = '192.168.1.1'
    vim_1.duAau.networkLayer.ip_addr = '192.168.1.10'         # eth0
    vim_1.vnfList[0].networkLayer.ip_addr = '192.168.1.11'    # eth1
    vim_1.switch.networkLayer.routing_table['192.168.1.11'] = vim_1.switch.ethernetPhyList[1].name
    vim_1.switch.networkLayer.routing_table['192.168.2.11'] = vim_1.switch.ethernetPhyList[0].name
    net.add_module(vim_1)
    
    
    vim_2 = GroundServerBase('Vim2', init_time, [113.5178, 34.7614, 0.0])
    vim_2.switch.networkLayer.ip_addr = '192.168.2.1'
    vim_2.duAau.networkLayer.ip_addr = '192.168.2.10'         # eth0
    vim_2.vnfList[0].networkLayer.ip_addr = '192.168.2.11'    # eth1
    vim_2.switch.networkLayer.routing_table['192.168.2.11'] = vim_2.switch.ethernetPhyList[1].name
    vim_2.switch.networkLayer.routing_table['192.168.3.11'] = vim_2.switch.ethernetPhyList[0].name
    vim_2.switch.networkLayer.routing_table['192.168.0.4']  = vim_2.switch.ethernetPhyList[0].name
    net.add_module(vim_2)
    
    
    vim_3 = GroundServerBase('Vim3', init_time, [113.5256, 34.7678, 0.0])
    vim_3.switch.networkLayer.ip_addr = '192.168.3.1'
    vim_3.duAau.networkLayer.ip_addr = '192.168.3.10'         # eth0
    vim_3.vnfList[0].networkLayer.ip_addr = '192.168.3.11'    # eth1
    vim_3.switch.networkLayer.routing_table['192.168.3.11'] = vim_3.switch.ethernetPhyList[1].name
    vim_3.switch.networkLayer.routing_table['192.168.0.2']  = vim_3.switch.ethernetPhyList[0].name
    net.add_module(vim_3)
    
    
    crowd_1 = CrowdBase("Crowd1", init_time, 2, [113.5100, 34.7550, 0.0], 100*u.m)
    
    ue_1_name = list(crowd_1.pedestrians.keys())[0]
    ue_1 = crowd_1.pedestrians[ue_1_name]
    ue_1.name = 'Ue1'
    ue_1.networkLayer.ip_addr = '192.168.0.1'
    net.add_module(ue_1)
    
    ue_3_name = list(crowd_1.pedestrians.keys())[1]
    ue_3 = crowd_1.pedestrians[ue_3_name]
    ue_3.name = 'Ue3'
    ue_3.networkLayer.ip_addr = '192.168.0.3'
    net.add_module(ue_3)

    
    crowd_2 = CrowdBase("Crowd2", init_time, 2, [113.5256, 34.7678, 0.0], 100*u.m)
    ue_2_name = list(crowd_2.pedestrians.keys())[0]
    ue_2 = crowd_2.pedestrians[ue_2_name]
    ue_2.name = 'Ue2'
    ue_2.networkLayer.ip_addr = '192.168.0.2'
    ue_2.appLayer.clocktime.stop()
    net.add_module(ue_2)
    
    ue_4_name = list(crowd_2.pedestrians.keys())[1]
    ue_4 = crowd_2.pedestrians[ue_4_name]
    ue_4.name = 'Ue4'
    ue_4.networkLayer.ip_addr = '192.168.0.4'
    ue_4.appLayer.clocktime.stop()
    net.add_module(ue_4)
    
    radio_medium = RadioMedium('RadioMedium')
    net.add_module(radio_medium)
    net.ready_for_medium(radio_medium)
    
    controller_global = ControllerGlobal4Crowd('Controller', net)
    net.add_module(controller_global)
    
    # net.draw_flag = True
    # net.draw_time_accuracy = 1*u.ms
    # net.draw_region = [113.5100-0.0015, 
    #                    113.5256+0.0015, 
    #                    34.7550-0.0015, 
    #                    34.7678+0.0015]
    net.run(until=1000 * u.ms)
    
    controller_global.draw_results()

